package com.zzl.bonds.ticket.controller.feign.plat;


import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.bonds.ticket.constant.status.TicketStatusEnum;
import com.zzl.bonds.ticket.dto.StatisticsDataDto;
import com.zzl.bonds.ticket.dto.credit.AddOrUpdateCreditDto;
import com.zzl.bonds.ticket.dto.credit.CoreEnterpriseCreditPage;
import com.zzl.bonds.ticket.dto.feign.ApprovalSubmitDto;
import com.zzl.bonds.ticket.dto.ticketopen.OpenDetailDto;
import com.zzl.bonds.ticket.entity.SubsidiaryQuotaEntity;
import com.zzl.bonds.ticket.service.CoreEnterpriseCreditService;
import com.zzl.bonds.ticket.service.SubsidiaryQuotaService;
import com.zzl.bonds.ticket.service.TicketOpenService;
import com.zzl.bonds.ticket.service.TicketService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author : spp
 * @author : gull
 * @date : 2022/3/10 09:50
 * ticket 服务 对外提供的 接口
 * @date : 2022/3/9 14:22
 */
@ApidocController("运营平台提供开单接口接口")
@RestController
@RequestMapping("ticketOpen/feign")
@Slf4j
public class OpenController extends WebBaseCtrl {

    @Autowired
    private TicketOpenService ticketOpenService;

    @Autowired
    private TicketService ticketService;

    @Autowired
    private CoreEnterpriseCreditService coreEnterpriseCreditService;

    @Autowired
    private SubsidiaryQuotaService subsidiaryQuotaService;

    /**
     * 分页查询开单待平台审批
     *
     * @param params 查询参数
     * @return 分页数据
     */
    @GetMapping("/ticketOpenApproveList")
    public Result ticketOpenApproveList(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = ticketOpenService.queryOpenApprovePage(params);
        return Result.ok().put("page", pageUtils);
    }

    /**
     * 信息
     */
    @GetMapping("/approveInfo")
    @ApidocOperation(description = "审批的的详情信息", operationId = "/ticket/ticketopen/approveInfo")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = OpenDetailDto.class)
    })
    public Result approveInfo(@ApidocParam(name = "开立申请表id", required = true) @RequestParam("id") String id) {
        try {
            return Result.ok().put("data", ticketOpenService.getOpenDetail(id, this.getUser()));
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    @PostMapping("/openTicketApproveSubmit")
    @ApidocOperation(description = "开立审批提交", operationId = "/ticket/ticketopen/openTicketApproveSubmit")
    @ApidocReturnR
    public Result openTicketSignSubmit(@ApidocParam(name = "开立审批提交", required = true) @Validated @RequestBody ApprovalSubmitDto checkSubmit) {
        try {
            ticketOpenService.openTicketApproveSubmit(checkSubmit, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * 我的凭证管理
     */

    /**
     * 平台 查询我的凭证
     */
    @GetMapping("/queryAllPage")
    public Result queryAllPage(@RequestParam Map<String, Object> params) {
        try {
            return Result.ok().put("page", ticketService.queryAllPage(params));
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * 平台 票据状态枚举值
     */
    @GetMapping("/getTicketStatusList")
    public Result getTicketStatus() {
        return Result.ok().put("list", TicketStatusEnum.getTicketStatusList());
    }

    /**
     * 冻结我的凭证
     */
    @GetMapping("/frozenMyTicket/{id}")
    public Result frozenMyTicket(@PathVariable("id") String id) {
        return Result.ok().message(ticketService.frozenTicket(id, this.getUser()));
    }

    /**
     * 平台 我的凭证--详情
     */
    @GetMapping("/getPltMyTicketDetail/{id}")
    public Result getPltMyTicketDetail(@PathVariable("id") String id) {
        try {
            return Result.ok().put("data", ticketService.getPltMyTicketDetail(id));
        } catch (SysException e) {
            return Result.error().message(e.getMsg());
        }
    }

    /**
     * 核心企业授信
     */

    /**
     * 分页查询核心企业授信授信
     */
    @GetMapping("/list")
    @ApidocOperation(description = "分页核心企业授信", operationId = "21101")
    @ApidocReturnPage(genericTypeClass = CoreEnterpriseCreditPage.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = {
            "creditNo 额度编号", "enterpriseId 授信主体", "financeInstitutyId 金融机构"
            , "effectiveStartTime 有效期开始日期", "effectiveEndTime 有效期结束日期", "creditStatus 授信状态，全部时，不传入值"
    }) @RequestParam Map<String, Object> params) {
        params.put("plat", "plat");
        PageUtils page = null;
        try {
            page = coreEnterpriseCreditService.queryPage(params, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }

        return Result.ok().put("page", page);
    }

    @GetMapping("/getData")
    @ApidocOperation(description = "三个统计数量", operationId = "ticket/coreCredit/getData11")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", genericTypeClass = StatisticsDataDto.class)
    })
    public Result getTodayOpenStatistics() {
        return Result.ok().put("data", coreEnterpriseCreditService.getOpenData(this.getUser()));
    }


    @PostMapping("/addUpdateCoreCredit")
    @ApidocOperation(description = "新增修改授信额度", operationId = "/ticket/ticketopen/addUpdateCoreCredit")
    @ApidocReturnR
    public Result addUpdateCoreCredit(@ApidocParam(name = "", required = true) @Validated @RequestBody AddOrUpdateCreditDto dto) {
        try {
            coreEnterpriseCreditService.addUpdateCoreCredit(dto, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            return Result.error(e.getMsg());
        }
    }


    /**
     * 子公司的额度上限
     */
    @GetMapping("/subInfo/{id}")
    @ApidocOperation(description = "子公司的额度上限", operationId = "subInfo")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "list", valueClass = SubsidiaryQuotaEntity.class)
    })
    public Result subInfo(@ApidocParam(name = "核心企业授信表id", required = true) @PathVariable("id") String id) {
        try {
            return Result.ok().put("list", subsidiaryQuotaService.getSubQuotaList(id));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }

    }

}
